package algorithm.action;

public class MoreThanHalfNum {

	public static void main(String[] args) {
		int[] arr = { 1, 2, 3, 2, 4, 2, 5, 2, 3 };
		System.out.println(moreThanHalfNum(arr));
	}

	static int moreThanHalfNum(int[] arr) {
		if(arr.length == 0) return 0;
		int num = arr[0];
		int count = 1;
		for(int i = 1; i < arr.length; i++) {
			if(count == 0) {
				num = arr[i];
				count = 1;
			}
			if(arr[i] == num) count ++;
			else count--;
		}
		
		count = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == num) count++;
		}
		if (count * 2 > arr.length) return num;
		return 0;
	}

}
